package com.example.smart_city_0829.activity.commonweal;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.commonweal.CommonwealActivityType;
import com.example.smart_city_0829.entity.commonweal.CommonwealBanner;
import com.example.smart_city_0829.entity.commonweal.CommonwealRecommendList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeGv;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class CommonwealMainActivity extends BaseActivity {

    private EditText et;
    private Banner banner;
    private MaxSizeGv gv;
    private ListView lv;
    private TextView recommendTv;
    private Button all;



    private ActivityTypeGvAdapter gvAdapter;
    private RecommendListAdapter lvAdapter;

    private List<CommonwealActivityType.DataBean> data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_commonweal_main;
    }

    @Override
    protected void initView() {
        et = (EditText) findViewById(R.id.et);
        banner = (Banner) findViewById(R.id.banner);
        gv = (MaxSizeGv) findViewById(R.id.gv);
        lv = (ListView) findViewById(R.id.lv);
        recommendTv = (TextView) findViewById(R.id.recommendTv);
        all = (Button) findViewById(R.id.all);

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        recommendTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    @Override
    protected void initData() {
        api.getWelfareBannerList().enqueue(new NetCallBack<CommonwealBanner>() {
            @Override
            public void onResponse(Call<CommonwealBanner> call, Response<CommonwealBanner> response) {
                final List<CommonwealBanner.DataBean> data = response.body().getData();
                banner.setImages(data.stream()
                        .map(dataBean -> Constant.BASE_URL + dataBean.getImgUrl())
                        .collect(Collectors.toList()));
                banner.setBannerTitles(data.stream()
                        .map(CommonwealBanner.DataBean::getTitle)
                        .collect(Collectors.toList()));
                banner.start();
                banner.startAutoPlay();
                banner.setDelayTime(3000);
            }
        });

        api.getWelfareActivityType().enqueue(new NetCallBack<CommonwealActivityType>() {
            @Override
            public void onResponse(Call<CommonwealActivityType> call, Response<CommonwealActivityType> response) {
                data = response.body().getData();
                if (gvAdapter == null){
                    gvAdapter = new ActivityTypeGvAdapter(data.subList(0,4),CommonwealMainActivity.this);
                    gv.setAdapter(gvAdapter);
                }else {
                    gvAdapter.resetData(data);
                    gvAdapter.notifyDataSetChanged();
                }
            }
        });

        api.getWelfareRecommendList().enqueue(new NetCallBack<CommonwealRecommendList>() {
            @Override
            public void onResponse(Call<CommonwealRecommendList> call, Response<CommonwealRecommendList> response) {
                final List<CommonwealRecommendList.RowsBean> rows = response.body().getRows();
                if (lvAdapter == null){
                    lvAdapter = new RecommendListAdapter(rows,CommonwealMainActivity.this);
                    lv.setAdapter(lvAdapter);
                }else {
                    lvAdapter.resetData(rows);
                    lvAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    startSearch(et.getText().toString());
                    return true;
                }
                return false;
            }
        });

        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context)
                        .load(o)
                        .transform(new RoundedCorners(8),new RoundedCorners(8))
                        .into(imageView);
            }
        });

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final CommonwealActivityType.DataBean dataBean = data.get(position);
                final Intent intent = new Intent(CommonwealMainActivity.this, CommonwealTypeActivity.class);
                intent.putExtra("typeId",dataBean.getId());
                startActivity(intent);
            }
        });

        recommendTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                migrateTo(CommonwealAllActivity.class);
            }
        });
    }

    private void startSearch(String targetKey) {
        et.setText("");
        final Intent intent = new Intent(CommonwealMainActivity.this,CommonwealSearchActivity.class);
        intent.putExtra("key",targetKey);
        startActivity(intent);
    }

    @Override
    protected String getActionBarTitle() {
        return "爱心公益";
    }

    private final class ActivityTypeGvAdapter extends CommonAdapter<CommonwealActivityType.DataBean,GVViewHolder>{

        public ActivityTypeGvAdapter(List<CommonwealActivityType.DataBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(CommonwealActivityType.DataBean data, int position, GVViewHolder viewHolder, View convertView, Context context) {
            Glide.with(CommonwealMainActivity.this)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .transform(new RoundedCorners(20),new RoundedCorners(20))
                    .into(viewHolder.im);
            viewHolder.serverName.setText(String.valueOf(data.getName()));
        }

        @Override
        protected GVViewHolder getViewHolder() {
            return new GVViewHolder();
        }

        @Override
        protected void initView(GVViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.serverName = (TextView) convertView.findViewById(R.id.server_name);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.server_list_item;
        }
    }

    private static class GVViewHolder{
        ImageView im;
        TextView serverName;
    }

    private final class RecommendListAdapter extends CommonAdapter<CommonwealRecommendList.RowsBean,LVViewHolder>{

        public RecommendListAdapter(List<CommonwealRecommendList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(CommonwealRecommendList.RowsBean data, int position, LVViewHolder viewHolder, View convertView, Context context) {
            Glide.with(CommonwealMainActivity.this)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
            viewHolder.title.setText(data.getName());
            viewHolder.content.setText("捐赠人数："+data.getDonateCount()+"\n"+"已筹善款："+data.getMoneyTotal());
            viewHolder.tag2.setText("发布组织："+data.getAuthor());
        }

        @Override
        protected LVViewHolder getViewHolder() {
            return new LVViewHolder();
        }

        @Override
        protected void initView(LVViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
        }
    }

    private static class LVViewHolder{
        ImageView im;
        TextView title;
        TextView content;
        TextView tag1;
        TextView tag2;
        TextView tag3;
    }
}